#include "NetworkMessage.h"

void NetworkMessage::AddString(std::string str)
{
	int len = str.length();
	if (!CanAdd(len + 2))
	{
		return;
	}
	Add2Bytes(len);
	AddRaw(str.c_str(), len);
}

void NetworkMessage::AddRaw(const char* data, int len)
{
	if (CanAdd(len))
	{
		memcpy(Buffer + Position, data, len);
		Position += len;
		Length += len;
	}
}

std::string NetworkMessage::GetString()
{
	int len = Get2Bytes();
	char* buf = new char[len + 1];
	buf[len] = 0; // NULL terminate
	std::string ret;
	GetRaw(buf, len);
	ret = (const char*)buf;
	delete buf;
	return ret;
}

void NetworkMessage::GetRaw(char* data, int len)
{
	memcpy(data, Buffer + Position, len);
	Position += len;
}

void NetworkMessage::AddByte(unsigned char d)
{
	if (!CanAdd(1))
	{
		return;
	}
	Buffer[Position++] = d;
	Length++;
}

unsigned char NetworkMessage::GetByte()
{
	return Buffer[Position++];
}